package com.guw.Swing;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {
    public static void main(String[] args) {
        new ImageIconDemo();
    }

    public ImageIconDemo(){
        //新建一个标签
        JLabel label = new JLabel("ImageIcon");
        //新建一个存放图片的URL
        //m1.png放在\src\com\guw\Swing目录下才生效
        URL url1 = ImageIconDemo.class.getResource("m1.png");
        //新建一个图标
        ImageIcon imageIcon = new ImageIcon(url1);
        //在标签中添加图标
        label.setIcon(imageIcon);
        //设置标签的文本水平对齐方式为居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //新建一个容器,方法名:获取窗体的内部窗格
        Container container = getContentPane();
        //在容器内添加标签
        container.add(label);
        //设置容器可见,边界大小,关闭退出
        setVisible(true);
        setBounds(100,100,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}